package org.mogu;

import java.util.ArrayList;

import org.mogu.adapter.ImageGroupAdapter;
import org.mogu.db.DBhelper;
import org.mogu.service.InfoSync;
import org.mogu.staticvalue.Convert;

import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.Spinner;
import android.widget.TextView;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView;
import android.view.View;

public class ImageGroup extends Activity implements AdapterView.OnItemSelectedListener {
	ImageGroup imageGroup;
	SQLiteDatabase readdb = null;
	Intent intent;
	String imgstr="[*sys/%s/%s*]";
	ArrayList<String> groupidlist=new ArrayList<String>();
	ArrayList<String> groupnamelist=new ArrayList<String>();
	ImageGroupAdapter adapter;
	Spinner spin;
	TextView text;
	GridView gridview ;
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		imageGroup=this;
		intent=this.getIntent();  
		setContentView(R.layout.imgliblist);
		spin=(Spinner)findViewById(R.id.spinner);
		spin.setOnItemSelectedListener(this);
		text=(TextView)findViewById(R.id.spinnertext);
		gridview = (GridView) findViewById(R.id.gridview);// 找到main.xml中定义gridview
		readdb = DBhelper.getDBHelper(this).getReadableDatabase();
		synchronized (InfoSync.class) {
		String[] una={Convert.uname};
		Cursor imglist = readdb.rawQuery(
				"select groupid,name from photoGroup where username=? ", una);
		imglist.moveToFirst();
		while (!imglist.isAfterLast()) {
			groupidlist.add(imglist.getString(0));
			groupnamelist.add(imglist.getString(1));
			imglist.moveToNext();
		}
		imglist.close();
		readdb.close();
		}
		ArrayAdapter<String> aa=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,groupnamelist);
		aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spin.setAdapter(aa);
		adapter=new ImageGroupAdapter(this,groupidlist.get(0));							// 的id
		gridview.setAdapter(adapter);// 调用ImageAdapter.java
		gridview.setOnItemClickListener(new OnItemClickListener() {// 监听事件
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
//				Toast.makeText(imageGroup, "" + position, Toast.LENGTH_SHORT)
//						.show();// 显示信息;
				String[] arr=adapter.getImgArr(position);
				onBack(String.format(imgstr, arr[0],arr[1]));
			}
		});
	}
	
	public void onBack(String str){
		Bundle bundle = new Bundle();  
        bundle.putString("data",str);  
        
        /*将Bundle对象assign给Intent*/ 
        intent.putExtras(bundle); 
		this.setResult(RESULT_OK, intent); 
		this.finish();
	}
	
	public void changeAdapter(String id){
		adapter=new ImageGroupAdapter(this,id);							// 的id
		gridview.setAdapter(adapter);// 调用ImageAdapter.java
	}

	@Override
	public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
			long arg3) {
		// TODO Auto-generated method stub
		changeAdapter(groupidlist.get(arg2));
	}

	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub
		
	}
//	public void onResume() {
//	    super.onResume();
//	    MobclickAgent.onResume(this);
//	}
//	public void onPause() {
//	    super.onPause();
//	    MobclickAgent.onPause(this);
//	}
}
